﻿using UnityEngine;
using System.Collections;
class ObjSel : MonoBehaviour
{
   static  private GameObject m_objSelBefore;
    static public UnityEngine.GameObject ObjSelBefore
    {
        get { return m_objSelBefore; }
        set { m_objSelBefore = value; }
    }

    private int m_iClickNum = 0;
    public float m_fIntervalTimeOneClick = 0.2f;
    public float m_fIntervalTimeDoubleClick = 0.2f;
    float m_fTimeFirst,m_fTimeSecond, m_TimeUp, m_TimeDown;
    CNetDataExchange m_pNetExchange;
    void Awake()
    {
        m_pNetExchange = CNetDataExchange.GetInstance();
    }
    void SelObject()
    {
        Vector3 mPos = Input.mousePosition;
        //向物体发射射线  
        Ray mRay = Camera.main.ScreenPointToRay(mPos);
        RaycastHit mHit;
        //射线检验  
        if (Physics.Raycast(mRay, out mHit))
        {
            //Cube  
            GameObject objSel = mHit.collider.gameObject;
            string strName = objSel.name;
            //  if (objSel.tag != "Terrian")
            {
                if (ObjSelBefore != objSel)
                {
                    if (ObjSelBefore)
                    {
                        Destroy(ObjSelBefore.GetComponent<WireFrameLineRenderer>());
                    }
                    objSel.AddComponent<WireFrameLineRenderer>();
                    ObjSelBefore = objSel;
                    m_pNetExchange.PushNewData(SendFunction.SendObjectSel(objSel.name));

                }
            }
            print(strName);
        }
    }
    void Update()
    {
        //获取鼠标位置  
        if (Input.GetMouseButtonDown(0))
        {
            m_TimeDown = Time.time;
        }
        if (Input.GetMouseButtonUp(0))
        {
            m_TimeUp = Time.time;
            if (m_TimeUp - m_TimeDown < m_fIntervalTimeOneClick)
            {
                ++m_iClickNum;
            }
            else
            {
                m_iClickNum = 0;
            }
            if (m_iClickNum == 1)
            {
                m_fTimeFirst = m_TimeDown;
            }
            else if (m_iClickNum == 2)
            {
                if (m_TimeDown - m_fTimeFirst < m_fIntervalTimeDoubleClick)
                {
                    m_fTimeSecond = m_TimeDown;
                }
                else
                {
                    m_iClickNum = 1;
                    m_fTimeFirst = m_TimeDown;
                }
            }
        }

        if (m_iClickNum == 2)
        {
            SelObject();
            m_iClickNum = 0;
        }

    }

    //void OnGUI()
    //{
    //    if(m_objSelBefore !=null)
    //    {
    //        ObjRelationUI obj = xmlConfigure.GetInstance().GetObjUI(m_objSelBefore.name);
    //        if (obj != null)
    //        {
    //            if (obj.UI != null)
    //            {
    //                obj.UI.Template.CreateUI();
    //            }
    //        }
    //    }
        
    //}
}


